package com.dc.cron;


import com.dc.constant.Constants;
import com.dc.dao.ShipRepairDao;
import com.dc.dao.WorkflowElectricBoxDao;
import com.dc.dao.http.ElectricIdNameDao;
import com.dc.entity.ShipRepairEntity;
import com.dc.entity.WorkflowElectricBoxEntity;
import com.dc.entity.http.ElectricIdNameEntity;
import com.dc.model.AjaxResult;
import com.dc.service.HttpService2;
import com.dc.service.ShipRepairService;
import com.dc.service.WorkflowElectricService;
import com.dc.service.dto.WorkflowElectricBoxDto;
import com.dc.service.dto.WorkflowPowerDto;
import com.dc.service.dto.electronic.HttpGetIdNameReturnNewBody;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class ElectricMachineMonthCron {

    @Resource
    HttpService2 httpService2;

    @Resource
    ElectricIdNameDao electricIdNameDao;

    @Resource
    ShipRepairDao shipRepairDao;

    @Resource
    WorkflowElectricBoxDao workflowElectricBoxDao;

    @Resource
    WorkflowElectricService workflowElectricService;


    // 每天凌晨更新 token
    @PostConstruct
    @Scheduled(cron = "0 0 0 * * ?")
    public void refreshToken() {
        httpService2.token = httpService2.getToken();
    }

    // 每天凌晨1点更新 电表id， 电表name
    @Scheduled(cron = "0 0 1 * * ?")
//    @PostConstruct
    public void refreshMfids() {

        List<HttpGetIdNameReturnNewBody> electricIdNames = httpService2.getElectricIdNames();

        List<ElectricIdNameEntity> collect = electricIdNames.stream().map(t -> new ElectricIdNameEntity(null, t.getMId(), t.getName(), t.getMfId(), t.getPName())).collect(Collectors.toList());

        collect.forEach(t->{
            String mfId = t.getMfId();
            ElectricIdNameEntity idNameEntity = electricIdNameDao.findByMfId(mfId);
            if(ObjectUtils.isNotEmpty(idNameEntity)) {      // 数据库存在
                if(!t.getName().equals(idNameEntity.getName())) {
                    t.setId(idNameEntity.getId());
                    electricIdNameDao.save(t);
                    httpService2.insertLog(idNameEntity, t);
                }
            } else {                                        // 数据库不存在
                electricIdNameDao.save(t);
            }
        });
    }

    @Scheduled(cron = "0 0 6,12,18 * * ?")
    public void refreshShipRepairElectric() {
        List<ShipRepairEntity> repairsNotComplete = shipRepairDao.findByComplete(Constants.COMPLETE_NO);
        if(ObjectUtils.isEmpty(repairsNotComplete)) {   // 都是完成的，不更新
            return;
        }

        // 有内容
        repairsNotComplete.forEach(t->{
            Long repairId = t.getId();

            if(Constants.containsSpecialShipRepairId(repairId)) {
                return;  // 特殊的维修单，不刷新
            }

            // 处理电箱数据 更新电箱电量
            List<WorkflowElectricBoxEntity> electricBoxEntities = workflowElectricBoxDao.findByShipRepairEntity(t);
            if(ObjectUtils.isEmpty(electricBoxEntities)) {
                return; // 本维修单的电箱列表为空，则跳过本次循环
            }
            Set<String> mfids = electricBoxEntities.stream().map(WorkflowElectricBoxEntity::getMfid).filter(ObjectUtils::isNotEmpty).collect(Collectors.toSet());
            Date enterTime = t.getEnterTime();
            Date outTime = t.getOutTime();
            List<WorkflowPowerDto> workflowPowerDtos = workflowElectricService.syncChildPower(mfids, enterTime, outTime);

            workflowPowerDtos.forEach(bean-> {
                workflowElectricBoxDao.updatePowerByMfid(bean.getMfid(), bean.getPower());
            });
        });

    }


    @Scheduled(cron ="0 0 2 * * ?")         // 每天凌晨两点调用
    public void sayHello() {
        System.out.println(System.currentTimeMillis() + ": hello world");
    }
}
